var express = require("express");
var router = express.Router();
const getTime = require("../../utils/getTime");
const orderModel = require("../../models/orderModel");

// 获取订单列表
router.get("/orders", (req, res) => {
  let { pageNo, pageSize } = req.query;
  if (pageNo === "undefined" || pageSize === "undefined") {
    orderModel
      .find()
      .populate("items.productId")
      .populate("items.charProductId")
      .select({ _v: 0 })
      .sort({ createdAt: -1 })
      .then((data) => {
        if (data.length === 0) {
          return res.json({
            code: 200,
            msg: "数据为空",
            data: data,
          });
        }
        return res.json({
          code: 200,
          msg: "获取订单列表成功",
          data: data,
          total: data.length,
        });
      })
      .catch((err) => {
        return res.json({
          code: "1001",
          msg: "数据库读取失败~~~",
          data: null,
        });
      });
  } else {
    orderModel
      .find()
      .then((result) => {
        if (result.length === 0) {
          return res.json({
            code: 200,
            msg: "数据为空",
            data: null,
          });
        } else {
          orderModel
            .find()
            .populate("items.productId")
            .populate("items.charProductId")
            .select({ _v: 0 })
            .sort({ createdAt: -1 })
            .skip((pageNo - 1) * pageSize)
            .limit(pageSize)
            .then((data) => {
              let total = result.length;
              return res.json({
                code: 200,
                msg: "获取订单列表成功",
                data: data,
                total: total,
              });
            })
            .catch((err) => {
              return res.json({
                code: "1001",
                msg: "数据库读取失败~~~",
                data: null,
              });
            });
        }
      })
      .catch((err) => {
        return res.json({
          code: "1001",
          msg: "数据库读取失败~",
          data: null,
        });
      });
  }
});

// 更改订单状态
router.patch("/order/:_id", (req, res) => {
  const { _id } = req.params;
  const { status } = req.body;
  orderModel
    .updateOne({ _id }, { status })
    .then((data) => {
      if (!data.acknowledged) {
        return res.json({
          code: -1,
          msg: "发货失败",
          data: null,
        });
      }
      return res.json({
        code: 200,
        msg: "发货成功~",
        data: null,
      });
    })
    .catch((err) => {
      return res.json({
        code: "1001",
        msg: "数据库读取失败~~~",
        data: null,
      });
    });
});

// 查询订单
router.get("/order/search", (req, res) => {
  let { keyword } = req.query;
  const regex = new RegExp(keyword, "i"); // 'i'表示不区分大小写
  orderModel
    .find({
      $or: [
        { "items.productName": { $regex: regex } },
        { status: { $regex: regex } },
        { orderNumber: { $regex: regex } },
        { userName: { $regex: regex } },
      ],
    })
    .select({ _v: 0 })
    .then((data) => {
      if (data.length === 0) {
        return res.json({
          code: 200,
          msg: "数据为空",
          data: data,
        });
      }
      return res.json({
        code: 200,
        msg: "查询成功",
        data: data,
        total: data.length,
      });
    })
    .catch((err) => {
      return res.json({
        code: "1001",
        msg: "数据库读取失败~~~",
        data: null,
      });
    });
});

module.exports = router;
